﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using Jayrock.Json;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Qik
{
    public static class QikExtensions
    {
        #region Qik

        internal static QikVideo ToQikVideo(this JsonObject obj)
        {
            return new QikVideo
            {
                Id = Convert.ToInt32(obj["id"]),
                UserId = Convert.ToInt32(obj["user_id"]),
                Title = Convert.ToString(obj["title"]),
                Url = Convert.ToString(obj["url"]),
                LargeThumbnailUrl = Convert.ToString(obj["large_thumbnail_url"]),
                SmallThumbnailUrl = Convert.ToString(obj["small_thumbnail_url"]),
                Filename = Convert.ToString(obj["filename"]),
                Duration = Convert.ToInt32(obj["duration"]),
                Views = Convert.ToInt32(obj["views"]),
                DeviceInfo = Convert.ToString(obj["device_info"]),
                IsLive = Convert.ToBoolean(obj["live"]),
                Features = ((JsonArray)obj["features"]).ToList<string>(),
                CreatedAt = DateTime.ParseExact(obj["created_at"].ToString(), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
                Country = Convert.ToString(obj["country"]),
                City = Convert.ToString(obj["city"]),
                Location = ((JsonArray)obj["location"]).ToQikLocation(),
                EmbedCode = Convert.ToString(obj["embed_html"]),
            };
        }

        internal static List<T> ToList<T>(this JsonArray arr) where T : class
        {
            var list = new List<T>();

            if (arr != null)
            {
                foreach (var itm in arr)
                    list.Add(itm as T);
            }

            return list;
        }

        internal static QikLocation ToQikLocation(this JsonArray arr)
        {
            return new QikLocation
            {
                Lattitude = Convert.ToDecimal(arr[0]),
                Longitude = Convert.ToDecimal(arr[1])
            };
        }

        #endregion

        #region UMP

        public static MediaItem ToMediaItem(this QikVideo qv)
        {
            var mi = new MediaItem
            {
                Id = qv.Id.ToString(),
                Title = qv.Title,
                PreviewImageUrl = qv.SmallThumbnailUrl,
                Icon = "mediaQik.png",
                HasChildren = false,
                Selectable = true
            };

            var durationTimeSpan = TimeSpan.FromSeconds(qv.Duration);
            var duration = durationTimeSpan.Seconds +" seconds";

            if (durationTimeSpan.Minutes > 0)
                duration = durationTimeSpan.Minutes + " minute" + ((durationTimeSpan.Minutes == 1)? " " : "s ") + duration;

            if (durationTimeSpan.Hours > 0)
                duration = durationTimeSpan.Hours + " hour" + ((durationTimeSpan.Hours == 1) ? " " : "s ") + duration;

            mi.MetaData.Add("Duration", duration);
            mi.MetaData.Add("Views", qv.Views.ToString());
            mi.MetaData.Add("Created", qv.CreatedAt.ToShortDateString() +" "+ qv.CreatedAt.ToShortTimeString());
            mi.MetaData.Add("Country", qv.Country);
            mi.MetaData.Add("City", qv.City);
            mi.MetaData.Add("Location", qv.Location.Lattitude +", "+ qv.Location.Longitude);

            return mi;
        }

        #endregion
    }
}